package com.snail.readyvr.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.event.GetSessionEvent;
import com.snail.readyvr.event.LoginEvent;
import com.snail.readyvr.glide.GlideUtils;
import com.snail.readyvr.mvp.model.User;
import com.snail.readyvr.rest.GetAccountInfoUtils;
import com.snail.readyvr.rest.GetSessionUtils;
import com.snail.readyvr.util.ACache;
import com.snail.readyvr.util.Constant;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.StringUtils;
import com.snail.readyvr.util.Utils;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class WelcomeActivity extends CheckPermissionsActivity {

    @BindView(R.id.welcome_img)
    ImageView mWelcomeImg;

    private boolean hasFinishCount = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        enableSystemBarTint(false,0,0);
        isFullScreen = true;
        super.onCreate(savedInstanceState);
        Utils.isLive = true;
        Object object = ACache.get(WelcomeActivity.this).getAsObject(Constant.ACacheKey.KEY_USER);
        if(object != null) {
            Utils.sUser = (User) object;
            Utils.token = Utils.sUser.getToken();
        }else
            Utils.token = null;
        if(Utils.sUser != null && !StringUtils.isBlank(Utils.sUser.getToken()))
            GetAccountInfoUtils.getAccountInfo(((MyApplication)getApplication()).getApplicationComponent().getLoginRestClient());

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void init() {


        GlideUtils.loadWelcomeImgFromDrawable(this,R.drawable.splash,mWelcomeImg);
        Observable.timer(3000, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                hasFinishCount = true;
                if(hasGetAllPermissions)
                    start();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }

    @Override
    protected void hasGetAllPermissions() {

        GetSessionUtils.getSession(((MyApplication) getApplication())
                .getApplicationComponent()
                .getRestClient(), GetSessionEvent.FROM_NORMAL);

        if (hasFinishCount)
            start();
    }

    private void start() {

        if(!StringUtils.isNull(Utils.token)){
            StartActivityUtils.startVideoPlayActivity(WelcomeActivity.this);
        }else {
            StartActivityUtils.startLoginActivity(WelcomeActivity.this, null, LoginEvent.FROM_LOGIN);
        }
        WelcomeActivity.this.finish();
    }
}
